// packagesA/pages/translation/index.js

const md5 = require('./libs/md5.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    fromarray: [{
        "text": "自动检测",
        "value": "auto"
      },
      {
        "text": "中文",
        "value": "zh"
      },
      {
        "text": "英语",
        "value": "en"
      },
      {
        "text": "粤语",
        "value": "yue"
      },
      {
        "text": "文言文",
        "value": "wyw"
      },
      {
        "text": "日语",
        "value": "jp"
      },
      {
        "text": "韩语",
        "value": "kor"
      },
      {
        "text": "法语",
        "value": "fra"
      },
      {
        "text": "西班牙语",
        "value": "spa"
      },
      {
        "text": "泰语",
        "value": "th"
      },
      {
        "text": "阿拉伯语",
        "value": "ara"
      },
      {
        "text": "俄语",
        "value": "ru"
      },
      {
        "text": "葡萄牙语",
        "value": "pt"
      },
      {
        "text": "德语",
        "value": "de"
      },
      {
        "text": "意大利语",
        "value": "it"
      },
      {
        "text": "希腊语",
        "value": "el"
      },
      {
        "text": "荷兰语",
        "value": "nl"
      },
      {
        "text": "波兰语",
        "value": "pl"
      },
      {
        "text": "保加利亚语",
        "value": "bul"
      },
      {
        "text": "爱沙尼亚语",
        "value": "est"
      },
      {
        "text": "丹麦语",
        "value": "dan"
      },
      {
        "text": "芬兰语",
        "value": "fin"
      },
      {
        "text": "捷克语",
        "value": "cs"
      },
      {
        "text": "罗马尼亚语",
        "value": "rom"
      },
      {
        "text": "斯洛文尼亚语",
        "value": "slo"
      },
      {
        "text": "瑞典语",
        "value": "swe"
      },
      {
        "text": "匈牙利语",
        "value": "hu"
      },
      {
        "text": "繁体中文",
        "value": "cht"
      },
      {
        "text": "越南语",
        "value": "vie"
      }
    ],
    toarray: [{
        "text": "英语",
        "value": "en"
      },
      {
        "text": "中文",
        "value": "zh"
      },
      {
        "text": "粤语",
        "value": "yue"
      },
      {
        "text": "文言文",
        "value": "wyw"
      },
      {
        "text": "日语",
        "value": "jp"
      },
      {
        "text": "韩语",
        "value": "kor"
      },
      {
        "text": "法语",
        "value": "fra"
      },
      {
        "text": "西班牙语",
        "value": "spa"
      },
      {
        "text": "泰语",
        "value": "th"
      },
      {
        "text": "阿拉伯语",
        "value": "ara"
      },
      {
        "text": "俄语",
        "value": "ru"
      },
      {
        "text": "葡萄牙语",
        "value": "pt"
      },
      {
        "text": "德语",
        "value": "de"
      },
      {
        "text": "意大利语",
        "value": "it"
      },
      {
        "text": "希腊语",
        "value": "el"
      },
      {
        "text": "荷兰语",
        "value": "nl"
      },
      {
        "text": "波兰语",
        "value": "pl"
      },
      {
        "text": "保加利亚语",
        "value": "bul"
      },
      {
        "text": "爱沙尼亚语",
        "value": "est"
      },
      {
        "text": "丹麦语",
        "value": "dan"
      },
      {
        "text": "芬兰语",
        "value": "fin"
      },
      {
        "text": "捷克语",
        "value": "cs"
      },
      {
        "text": "罗马尼亚语",
        "value": "rom"
      },
      {
        "text": "斯洛文尼亚语",
        "value": "slo"
      },
      {
        "text": "瑞典语",
        "value": "swe"
      },
      {
        "text": "匈牙利语",
        "value": "hu"
      },
      {
        "text": "繁体中文",
        "value": "cht"
      },
      {
        "text": "越南语",
        "value": "vie"
      }
    ],
    fromval: 'auto',
    toval: 'en',
    tranval: '',
    isfocus: false,
    tranresult: '',
    base64: '',
    access_token: ''
  },

  focuschange(e) {
    this.setData({
      isfocus: true
    })
  },

  blurchange() {
    this.setData({
      isfocus: false
    })
  },

  changefrom(e) {
    this.setData({
      fromval: e.detail
    })
    if (this.data.tranval) {
      this.gotrans()
    }
  },
  changeto(e) {
    this.setData({
      toval: e.detail
    })
    if (this.data.tranval) {
      this.gotrans()
    }
  },
  tranvalchange(e) {
    const self = this;
    const value = e.detail.value;
    this.setData({
      tranval: value
    });

    // 如果已经存在定时器，则清除
    if (this.timeout) {
      clearTimeout(this.timeout);
    }

    // 设置定时器，延迟执行函数
    this.timeout = setTimeout(() => {
      // 这里放置需要节流执行的函数
      console.log('用户停止输入，执行函数', self.data.tranval);
      //发起请求，执行翻译回调
      self.gotrans()

    }, 1000); // 延迟时间可以根据需要调整，这里设置为1000毫秒
  },
  gotrans() {
    if (!this.data.tranval) {
      return this.setData({
        tranresult: ''
      })
    }
    let that = this
    let sign = '20231113001878894' + this.data.tranval + '1435660288' + 'sS4bShWWtGhAi6bk_GOy'
    // 使用MD5加密
    // const encryptedText = CryptoJS.MD5(sign).toString();
    const encryptedText = md5(sign);
    let url = 'http://api.fanyi.baidu.com/api/trans/vip/translate?q=' + this.data.tranval + '&from=' + this.data.fromval + '&to=' + this.data.toval + '&appid=20231113001878894' + '&salt=1435660288' + '&sign=' + encryptedText
    wx.request({
      url: url,
      method: 'GET',
      success(res) {
        if (res.data.trans_result[0]) {
          that.setData({
            tranresult: res.data.trans_result[0].dst
          })
        }

      }
    })
  },

  gettoken() {
    let that = this
    wx.request({
      method: 'POST',
      url: `https://aip.baidubce.com/oauth/2.0/token?client_id=23knTVA66ZYFNy0fFBjxxyEL&client_secret=5PkKndCwWYxu5GoRBzPVwiHTlbAHhlX4&grant_type=client_credentials`,
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      },
      success(res) {
        that.setData({
          access_token: res.data.access_token
        })
      }
    })
  },

  canceltranval() {
    this.setData({
      tranval: '',
      tranresult: ''
    })
  },
  handlePaste() {
    let that = this
    wx.getClipboardData({
      success(res) {
        that.setData({
          tranval: res.data
        })
        that.gotrans()
      }
    })
  },
  handlecopy() {
    let that = this
    wx.setClipboardData({
      data: that.data.tranresult,
      success(res) {
        console.log(res);
      }
    })
  },
  

  goword() {
    wx.navigateTo({
      url: '/packagesA/pages/translation/word/index',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.gettoken()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})